package com.bjfu.college.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.college.dto.FilterCondition;
import com.bjfu.college.mapper.StudentInfoIntentionSchemeMapper;
import com.bjfu.college.pojo.StudentInfoIntentionScheme;
import com.bjfu.college.service.StudentInfoIntentionSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 87290
* @description 针对表【student_info_intention_scheme】的数据库操作Service实现
* @createDate 2023-07-13 17:37:39
*/
@Service
public class StudentInfoIntentionSchemeServiceImpl extends ServiceImpl<StudentInfoIntentionSchemeMapper, StudentInfoIntentionScheme>
    implements StudentInfoIntentionSchemeService{

    @Autowired
    private StudentInfoIntentionSchemeMapper studentInfoIntentionSchemeMapper;


    /**
     * @Description: 利用学生id,方案id以及过滤条件保存在中间表
     * @Author: Pzf
     * @Date:   2023/7/15 17:27
     * @Param:  [studentId, schemeId, filterCondition, isOverlaid(是否是覆盖原方案)]
     * @Return: boolean
     */
    @Override
    @Transactional
    public boolean saveCondition(int studentId, int schemeId, FilterCondition filterCondition, boolean isOverlaid){
        // 更新或者插入是否成功
        boolean isSuccess =false;
        // 对要插入的记录实例对象赋值
        StudentInfoIntentionScheme studentInfoIntentionScheme = new StudentInfoIntentionScheme();
        studentInfoIntentionScheme.setStudentId(studentId);
        studentInfoIntentionScheme.setSchemeId(schemeId);
        studentInfoIntentionScheme.setScoreBias(filterCondition.getScoreBias());
        studentInfoIntentionScheme.setMajorClass(JSON.toJSONString(filterCondition.getMajorCondition()));
        studentInfoIntentionScheme.setInstitutionalLevel(JSON.toJSONString(filterCondition.getCollegeLevelCondition()));
        studentInfoIntentionScheme.setEnrollmentCategory(JSON.toJSONString(filterCondition.getEnrollCategoryCondition()));
        studentInfoIntentionScheme.setOtherConditions(JSON.toJSONString(filterCondition.getOtherCondition()));
        studentInfoIntentionScheme.setProvince(JSON.toJSONString(filterCondition.getProvinceCondition()));
        studentInfoIntentionScheme.setSchoolProperty(JSON.toJSONString(filterCondition.getCollegeHoldCondition()));

        // 对筛选条件更新覆盖或者插入新的筛选条件
        if (isOverlaid){
            UpdateWrapper<StudentInfoIntentionScheme> studentInfoIntentionSchemeUpdateWrapper = new UpdateWrapper<>();
            studentInfoIntentionSchemeUpdateWrapper.eq("student_id",studentId).eq("scheme_id",schemeId);
            try {
                isSuccess = studentInfoIntentionSchemeMapper.update(studentInfoIntentionScheme, studentInfoIntentionSchemeUpdateWrapper) > 0 ? true : false;
                if (!isSuccess) throw new RuntimeException("过滤条件更新失败！");
            } catch (Exception e) {
                throw new RuntimeException("过滤条件更新失败！" + e.toString());
            }
        }else {
            try {
                isSuccess = studentInfoIntentionSchemeMapper.insert(studentInfoIntentionScheme) > 0 ? true : false;
                if (!isSuccess) throw new RuntimeException("过滤条件增加失败！");
            } catch (Exception e) {
                throw new RuntimeException("过滤条件增加失败！" + e.toString());
            }
        }
        return isSuccess;
    }

    /**
     * @Description: 根据方案id删除表【student_info_intention_scheme】中的记录
     * @Author: Pzf
     * @Date:   2023/7/16 18:04
     * @Param:  [schemeId]
     * @Return: int
     */
    @Override
    @Transactional
    public int deleteCondition(int schemeId){
        int result;
        UpdateWrapper<StudentInfoIntentionScheme> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("scheme_id",schemeId);
        try {
            result = studentInfoIntentionSchemeMapper.delete(updateWrapper);
        } catch (Exception e) {
            throw new RuntimeException("该方案的筛选条件记录删除失败");
        }
        return result;
    }

    /**
     * @Description: 通过学生ID获取当前学生的所有方案
     * @Author: Pzf
     * @Date:   2023/7/17 22:45
     * @Param:  [studentId]
     * @Return: java.util.List<com.bjfu.college.pojo.StudentInfoIntentionScheme>
     */
    @Override
    public List<StudentInfoIntentionScheme> getConditions(int studentId){
        QueryWrapper<StudentInfoIntentionScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        return studentInfoIntentionSchemeMapper.selectList(queryWrapper);
    }
    /**
     * @Description: 根据schemeId和学生Id返回中间表记录
     * @Author: Pzf
     * @Date:   2023/7/22 22:54
     * @Param:  [schemeId, studentId]
     * @Return: com.bjfu.college.pojo.StudentInfoIntentionScheme
     */
    @Override
    public StudentInfoIntentionScheme getCondition(int schemeId, int studentId){
        QueryWrapper<StudentInfoIntentionScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId).eq("scheme_id",schemeId);
        return studentInfoIntentionSchemeMapper.selectOne(queryWrapper);
    }

}




